; 004.c 

global _main

section .data
LC0:
	db "please input binary number:",0Ah,0
	
LC1:
	db "%s",0
	
LC2:
	db "ip:",0ah,0
	
LC3:
	db "%d.%d.%d.%d",0ah,0

extern _printf,_scanf 
section .text
bin_dec:
	push ebp
	mov ebp,esp
	sub esp,0x1000
	;[ebp+8] x
	;[ebp+12] n 
	
	cmp dword [ebp+12],0
	jne .Lbin_dec_400
	
	mov eax,1
	jmp .Lbin_dec_0 
	
.Lbin_dec_400:
	mov eax,[ebp+12] ;n
	sub eax,1
	mov [esp+4],eax 
	mov eax,[ebp+8] ; x 
	mov [esp+0],eax 
	call bin_dec 
	sub esp,8
	
	imul eax,[ebp+8] ;x 
	
.Lbin_dec_0:
	leave
	ret 
	
_main:
	push ebp
	mov ebp,esp
	sub esp,0x1000
	
	mov dword [ebp-0x1000],0 ; i 
	mov dword [ebp-0xffc],0 ; ip[0]
	mov dword [ebp-0xff8],0 ; ip[1]
	mov dword [ebp-0xff4],0 ; ip[2]
	mov dword [ebp-0xff0],0 ; ip[3]
	
	mov dword [ebp-0xfe0],0 ; a 
		
	mov dword [esp+0],LC0
	mov eax,0 
	call _printf 
	sub esp,4
	
	lea eax,[ebp-0xfe0] ; a
	mov [esp+4],eax 
	mov dword [esp+0],LC1 
	mov eax,0
	call _scanf
	sub esp,4
	
	mov dword [ebp-0x1000],0 ; i 
	jmp .Lmain_400
	
.Lmain_500:
	mov eax,[ebp-0x1000] ; i 
	lea edi, [ebp-0xfe0] ; a
	cmp byte [edi+eax],49 ; '1'
	jne .Lmain_490
	
	mov eax,7
	sub eax,[ebp-0x1000] ; i 
	mov [esp+4],eax 
	mov dword [esp+0],2 
	call bin_dec 
	sub esp,8
	mov ecx,eax 
	
	mov eax,0
	sal eax,2
	lea edi,[ebp-0xffc] ; ip 
	mov eax,[edi+eax] ; ip[0] 
	add ecx,eax 
	
	mov eax,0
	sal eax,2
	lea edi,[ebp-0xffc] ; ip 
	mov [edi+eax],ecx ; ip[0] 
	
.Lmain_490:
	add dword [ebp-0x1000],1 ; i 	
	
.Lmain_400:
	cmp dword [ebp-0x1000],8 ; i 
	jl .Lmain_500 
	
	mov dword [ebp-0x1000],8 ; i 
	jmp .Lmain_380
	
.Lmain_398:
	mov eax,[ebp-0x1000] ; i 
	lea edi,[ebp-0xfe0] ; a 
	cmp byte [edi+eax], 49 ; '1'
	jne .Lmain_382
	
	mov eax,15
	sub eax,[ebp-0x1000] ; i 
	mov [esp+4],eax 
	mov dword [esp+0],2 
	call bin_dec 
	sub esp,8
	mov ecx,eax 
	
	mov eax,1
	sal eax,2
	lea edi,[ebp-0xffc] ; ip 
	add [edi+eax],ecx ; ip[1]
		
.Lmain_382:
	add dword [ebp-0x1000],1 ; i 
	
.Lmain_380:
	cmp dword [ebp-0x1000], 16 ; i
	jl .Lmain_398
	
	mov dword [ebp-0x1000],16 ; i 
	jmp .Lmain_360
	
.Lmain_378:
	mov eax,[ebp-0x1000] ; i 
	lea edi,[ebp-0xfe0] ; a 
	cmp byte [edi+eax],49 ; '1'
	jne .Lmain_362
	
	mov eax,23
	sub eax,[ebp-0x1000] ; i 
	mov [esp+4],eax 
	mov dword [esp+0],2
	call bin_dec 
	sub esp,8
	mov ecx,eax 

	mov eax,2 
	sal eax,2
	lea edi,[ebp-0xffc] ; ip 
	add [edi+eax],ecx ; ip[2] 
	
.Lmain_362:
	add dword [ebp-0x1000],1 ; i 
	
.Lmain_360:
	cmp dword [ebp-0x1000],24 ; i 
	jl .Lmain_378
	
	mov dword [ebp-0x1000],24
	jmp .Lmain_300

.Lmain_358:
	mov eax,[ebp-0x1000] ; i 
	lea edi,[ebp-0xfe0] ; a 
	cmp byte [edi+eax],49 ; '1'
	jne .Lmain_350
	
	mov eax,31
	sub eax,[ebp-0x1000] ; i 
	mov [esp+4],eax 
	mov dword [esp+0],2
	call bin_dec
	sub esp,8
	mov ecx,eax 
	
	mov eax,3 
	sal eax,2
	lea edi,[ebp-0xffc] ; ip 
	add [edi+eax],ecx
	
.Lmain_350:
	cmp byte [edi+eax],0 ; '\0'
	jne .Lmain_320
	
	jmp .Lmain_0
	
.Lmain_320:
	add dword [ebp-0x1000],1 ; i 
	
.Lmain_300:
	cmp dword [ebp-0x1000],32 ; i 
	jl .Lmain_358
	
	mov dword [esp+0],LC2
	mov eax,0 
	call _printf 
	sub esp,4
	
	mov eax,3
	sal eax,2
	lea edi,[ebp-0xffc] ; ip 
	mov eax,[edi+eax] ; ip[3] 
	mov [esp+16],eax 
	
	mov eax,2
	sal eax,2
	lea edi,[ebp-0xffc] ; ip 
	mov eax,[edi+eax] ; ip[2] 
	mov [esp+12],eax 
	
	mov eax,1
	sal eax,2
	lea edi,[ebp-0xffc] ; ip 
	mov eax,[edi+eax] ; ip[1] 
	mov [esp+8],eax 
	
	mov eax,0
	sal eax,2
	lea edi,[ebp-0xffc] ; ip 
	mov eax,[edi+eax] ; ip[0] 
	mov [esp+4],eax 
	
	mov dword [esp+0],LC3 
	mov eax,0 
	call _printf 
	sub esp,20
	
.Lmain_0:
	mov eax,0
	leave
	ret 